/*
 * @lc app=leetcode.cn id=20 lang=javascript
 *
 * [20] 有效的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    const bracketsArr = []
    function tesrBrackets(previous, last) {
        if (previous === '(') {
            return last === ')'
        }
        if (previous === '[') {
            return last === ']'
        }
        if (previous === '{') {
            return last === '}'
        }
        return false
    }
    s.split('').forEach((e,i) => {
        if (bracketsArr.length === 0) {
            bracketsArr.push(e)
        } else {
            if(tesrBrackets(bracketsArr[bracketsArr.length - 1], e)) {
                bracketsArr.pop()
            } else {
                bracketsArr.push(e)
            }
        }
    })
    return bracketsArr.length === 0
    
};
// @lc code=end

